package windowUnits;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javaUtils.DateUtils;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

public class ButtonEditor extends DefaultCellEditor {
	
	protected JButton button;
	private String    label;
	private boolean   isPushed;
	private JTextField setField;
	private Calendar cal;
	private JFrame fr;
	
	public ButtonEditor(JTextField arg0,JTextField setField,Calendar cal,JFrame fr) {
		super(arg0);
		this.setField = setField;
		this.cal = cal;
		this.fr = fr;
		init();
	}

	public ButtonEditor(JCheckBox arg0,JTextField setField,Calendar cal,JFrame fr) {
		super(arg0);
		this.setField = setField;
		this.cal = cal;
		this.fr = fr;
		init();
	}

	public ButtonEditor(JComboBox arg0,JTextField setField,Calendar cal,JFrame fr) {
		super(arg0);
		this.setField = setField;
		this.cal = cal;
		this.fr = fr;
		init();
	}
	
	public void init(){
		button = new JButton();
	    button.setOpaque(true);
	    button.setBorder(new LineBorder(new Color(213,213,213), 1, false));
	    button.setBackground(new Color(232,232,232));
	    button.setFont(new Font("", Font.PLAIN, 10));
	    button.setMargin(new Insets(0, 0, 0, 0));
	    button.setBounds(0, 0, 18, 18);
	    button.addActionListener(new ActionListener() {
	      public void actionPerformed(ActionEvent e) {
	        fireEditingStopped();
	      }
	    });
	}
	
	public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {
		if (isSelected) {
			button.setForeground(table.getSelectionForeground());
			button.setBackground(table.getSelectionBackground());
		} else{
			button.setForeground(table.getForeground());
			button.setBackground(table.getBackground());
		}
		label = (value ==null) ? "" : value.toString();
		button.setText( label );
		isPushed = true;
		
		if(value.toString().length() == 0){
			button.setEnabled(false);
			button.setFocusable(false);
	    }else{
	    	button.setEnabled(true);
	    	button.setFocusable(true);
	    }
		
		return button;
	}
	public Object getCellEditorValue() {
	    if (isPushed)  {
	      // 
	      // 
	      //JOptionPane.showMessageDialog(button ,label + ": Ouch!");
	      System.out.println(label + ": Ouch!");
	      if(label != null){
	    	  Calendar tableDate = Calendar.getInstance();
	  		  tableDate.set(Integer.parseInt(DateUtils.getDateNow(DateUtils.DF_YYYY, cal)), Integer.parseInt(DateUtils.getDateNow(DateUtils.DF_MM, cal))-1, Integer.parseInt(label));
	    	  setField.setText(DateUtils.getDateNow(DateUtils.DATE_FORMAT_NOW, tableDate));
	    	  fr.dispose();
	      }
	    }
	    isPushed = false;
	    return new String( label ) ;
	  }
	   
	  public boolean stopCellEditing() {
	    isPushed = false;
	    return super.stopCellEditing();
	  }
	 
	  protected void fireEditingStopped() {
	    super.fireEditingStopped();
	  }
}
